﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using System.Linq;
using System.Text;

using ZhimeiSoft.Framework.Client.Utils;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Client.Manager;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    /// <summary>
    /// 预约控制类
    /// </summary>
    public class ReservationCtrl
    {
        /// <summary>
        /// 预约成功后插入代办事项
        /// </summary>
        /// <param name="employeeID"></param>
        /// <param name="customInfo"></param>
        /// <param name="reservationInfo"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public bool InsertToDoList(int employeeID, DBEntityCustomInfo customInfo, DBEntityReservation reservationInfo, string serviceNames, out string error)
        {
            error = string.Empty;
            DBEntityToDoList todoList = new DBEntityToDoList();
            todoList.EMPLOYEEID = employeeID;
            todoList.TODOCOMPLETIONRATE = 0;
            todoList.TODODECRIPTION = string.Concat("今日预约确认 会员：", customInfo.CNAME, "(", customInfo.CNICKNAME, ") 预约时间：", reservationInfo.RESERVATIONTIME.ToLongDateString(), " ",
                reservationInfo.RESERVATIONTIME.ToLongTimeString(), " 到店 ", " 会员电话：", customInfo.CMOBILEPHONE, " 护理项目：| ", serviceNames);

            DateTime drStartTime = new DateTime(reservationInfo.RESERVATIONTIME.AddDays(-1).Ticks);
            if (drStartTime <= DateTime.Now)
            {
                return true;
            }
            //生成任务开始时间
            todoList.TODOSTARTTIME = new DateTime(drStartTime.Year, drStartTime.Month, drStartTime.Day, 8, 30, 0);
            todoList.TODOENDTIME = new DateTime(drStartTime.Year, drStartTime.Month, drStartTime.Day, 23, 59, 59);
            todoList.TODOSTATE = DBEntityToDoList.ENUM_TODOSTATE.T0N;
            todoList.TODOMEMO = "此事项由预约生成";

            DBEntityReservationToDoList reservationToDoList = new DBEntityReservationToDoList();
            reservationToDoList.RESERVATIONID = reservationInfo.RESERVATIONID;
            
            return BussinessProxyManager.Instance.InsertResverationToDoList(todoList, reservationToDoList, out error);
        }

        /// <summary>
        /// 添加具体的预约记录
        /// </summary>
        public void addReservationDetail(DataTable dtReservationDetail, DBEntityServiceItem serviceItem, DBEntityCustomerServiceItem item, ref DataRow drNewRow)
        {
            DataRow drNew = dtReservationDetail.NewRow();

            drNew[DBEntityServiceItem.FieldName.SERVICEITEMID] = serviceItem.SERVICEITEMID;
            drNew[DBEntityServiceItem.FieldName.SNAME] = serviceItem.SNAME;
            drNew[DBEntityServiceItem.FieldName.SCATEGORYID] = serviceItem.SCATEGORYID;
            drNew[DBEntityServiceItem.FieldName.SPRICE] = serviceItem.SPRICE;
            drNew[DBEntityServiceItem.FieldName.STIMESPAN] = serviceItem.STIMESPAN;
            drNew[DBEntityServiceItem.FieldName.STATE] = serviceItem.STATE;
            drNew[DBEntityServiceItem.FieldName.SCALLBACKFLAG] = serviceItem.SCALLBACKFLAG;
            drNew[DBEntityServiceItem.FieldName.SCALLBACKDAY] = serviceItem.SCALLBACKDAY;
            drNew[DBEntityServiceItem.FieldName.STARTDATE] = serviceItem.STARTDATE;
            drNew[DBEntityServiceItem.FieldName.SENDDATE] = serviceItem.SENDDATE;
            drNew[DBEntityServiceItem.FieldName.SREMARK] = serviceItem.SREMARK;
            drNew[DBEntityServiceItem.FieldName.SAVAILABLEDAYS] = serviceItem.SAVAILABLEDAYS;
            drNew[DBEntityServiceItem.FieldName.SINDEXCODE] = serviceItem.SINDEXCODE;
            drNew[DBEntityServiceItem.FieldName.SERVICEITEMCODE] = serviceItem.SERVICEITEMCODE;
            drNew[DBEntityServiceItem.FieldName.SCOUNT] = serviceItem.SCOUNT;
            drNew[DBEntityCustomerServiceItem.FieldName.CREMAINCOUNT] = item.CREMAINCOUNT;

            //预约项目明细表
            drNew[DBEntityReservationServiceItem.FieldName.RESERVATIONSTATE] = DBEntityReservationServiceItem.ENUM_RESERVATIONSTATE.C0U;
            drNew[DBEntityReservationServiceItem.FieldName.PRESERVATIONID] = -1;
            drNew[DBEntityReservationServiceItem.FieldName.CUSTOMERSERVICEITEMID] = item.CUSTOMERSERVICEITEMID;
            dtReservationDetail.Rows.Add(drNew);
            drNewRow = drNew;
        }

        /// <summary>
        /// 返回预约项目所需的分钟
        /// </summary>
        /// <param name="dtReservationDetail"></param>
        /// <returns></returns>
        public int getResverationDetailTimeSpan(DataTable dtReservationDetail)
        {
            DataRow[] drDetails = dtReservationDetail.Select("", "", DataViewRowState.CurrentRows);
            int timeSpan = 0;
            foreach (DataRow dr in drDetails)
            {
                if (dr[DBEntityServiceItem.FieldName.STIMESPAN] == System.DBNull.Value)
                {
                    continue;
                }
                timeSpan += Convert.ToInt32(dr[DBEntityServiceItem.FieldName.STIMESPAN]);
            }
            return timeSpan;
        }

        /// <summary>
        /// 根据过滤条件返回员工ID
        /// </summary>
        /// <param name="dsEmployee">员工数据集</param>
        /// <param name="filter">过滤条件</param>
        /// <returns></returns>
        public int getEmployeeID(DataTable dsEmployee, string filter)
        {
            DataRow[] drEmployeeID = dsEmployee.Select(filter);
            if (drEmployeeID.Length == 0)
            {
                throw new Exception("没有找到员工的信息。");
            }
            return Convert.ToInt16(drEmployeeID[0][DBEntityEmployee.FieldName.EMPLOYEEID]);
        }
    }
}
